package com.xinchuang.controller.targetSystem.config;

import com.xinchuang.common.Result;
import com.xinchuang.common.ResultUtil;
import com.xinchuang.entity.targetSystem.config.SystemParam;
import com.xinchuang.entity.targetSystem.object.SystemParamAdd;
import com.xinchuang.entity.targetSystem.object.SystemSort;
import com.xinchuang.service.targetSystem.SystemSortService;
import com.xinchuang.service.targetSystem.config.SystemParamService;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @ClassName SystemConfigController
 * @Description: TODO(写一段描述的话)
 * @Author zhangxuan
 * @Date 2023-08-07 14:58
 * @Verion V1.0
 */
@Api(value = "系统配置", tags = {"系统配置"})
@RequestMapping(value = "/systemConfig")
@RestController
@RequiredArgsConstructor
public class SystemConfigController {

    private final SystemParamService systemParamService;

    private final SystemSortService systemSortService;

    private final String SYSTEM_PARAM_URL = "/systemParam";

    @PostMapping(value = SYSTEM_PARAM_URL)
    public Result addSystemParam(
            @RequestBody SystemParamAdd systemParamAdd
    ) {
        return systemParamService.addOrUpdateSystemParam(systemParamAdd);
    }

    @DeleteMapping(value = SYSTEM_PARAM_URL)
    public Result deleteSystemParam(
            @RequestParam(value = "id") String id
    ) {
        return systemParamService.deleteSystemParam(id);
    }

    @GetMapping(value = SYSTEM_PARAM_URL)
    public Result getSystemParamList(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(value = "id", required = false) String id,
            @RequestParam(value = "key", required = false) String key
    ) {
        if (StringUtils.isNotBlank(id)) {
            return systemParamService.selectSystemParamById(id);
        } else if (StringUtils.isNotBlank(key)) {
            return systemParamService.selectSystemParamByKey(key);
        }
        return systemParamService.selectSystemParam(pageNum, pageSize, null);
    }

    @PostMapping(value = "/sort")
    public Result getSystemSort(@RequestBody @Validated SystemSort systemSort) {
        return systemSortService.getSort(systemSort.getId(), systemSort.getType(), systemSort.getStatus());
    }

}